package exercises;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-09
 * Time: 16:51
 */

/**
 * 反转一个链表
 */

public class Exercise1 {
    static class ListNode{
        public int val;
        public ListNode next;
    }

    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }

        ListNode cur = head.next;
        head.next = null;//先把第一个的下一个，置为空
        while(cur != null) {
            ListNode curN = cur.next;
            //头插
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }
}
